//
//  SYNotiTableViewCell.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/5/26.
//  Copyright © 2016年 zz. All rights reserved.
//

#define TEACHERCELLID @"mycellid"  //我发布的
#define CLASSCELLID @"cellid"  //班级相关的

#import "SYNotiTableViewCell.h"
#import "SDPhotoBrowser.h"

@interface SYNotiTableViewCell()<SDPhotoBrowserDelegate>

@end

@implementation SYNotiTableViewCell{
    
    int count; //照片的数量
    
    NSArray *_imageArr;

}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        /*
         subLabel; //科目
         timeLabel; //事件
         contentLabel; //内容
         sourceLabel;  //来源
         icView *picImageView;
         */
        
        UIView *superView = self.contentView;
        
        //科目
        _subLabel = [[UILabel alloc] init];
        _subLabel.font = [UIFont systemFontOfSize:17];
        [superView addSubview:_subLabel];
        
        //时间
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = RGBACOLOR(59, 59, 59, 1);
        _timeLabel.textAlignment = NSTextAlignmentRight;
        
        [superView addSubview:_timeLabel];
        //内容
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:15];
        _contentLabel.textColor = RGBACOLOR(155, 155, 155, 1);
        _contentLabel.numberOfLines = 2;
        [superView addSubview:_contentLabel];
        
        //来源
        _sourceLabel = [[UILabel alloc] init];
        _sourceLabel.font = [UIFont systemFontOfSize:12];
        _sourceLabel.textColor = RGBACOLOR(59, 59, 59, 1);
        _sourceLabel.textAlignment=NSTextAlignmentLeft;
        [superView addSubview:_sourceLabel];
        
        //图片
        _picImageView = [[homeworkPicView alloc] init];
        _picImageView.userInteractionEnabled = YES;
        _picImageView.taskImageView.userInteractionEnabled = YES;
        
        //是否已读
        _isreadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _isreadBtn.hidden = YES;
        [_isreadBtn setImage:[UIImage imageNamed:@"new_tongji_nor"] forState:UIControlStateNormal];
        [superView addSubview:_isreadBtn];
        
        
        [superView addSubview:_picImageView];
        
        [_subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(superView.mas_left).offset(13);
            make.top.equalTo(superView.mas_top).offset(9);
            make.height.equalTo(@20);
//            make.right.equalTo(self->_timeLabel.mas_left).offset(-15);
            make.width.equalTo(@100);
            
        }];
        
        [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(superView.mas_top).offset(14);
            make.right.equalTo(self->_picImageView.mas_left).offset(-15);
            make.left.equalTo(self->_subLabel.mas_right).offset(5);

            make.height.equalTo(@12);
//            make.width.equalTo(@100);
        }];
        
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self->_subLabel.mas_bottom).offset(10);
            make.right.equalTo(self->_picImageView.mas_left).offset(-15);
            make.left.equalTo(superView.mas_left).offset(15);
            make.height.equalTo(@39);
        }];
        
        [_picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(superView.mas_top).offset(12);
            make.right.equalTo(superView.mas_right).offset(-12);
            make.height.equalTo(@87);
            make.width.equalTo(@87);
        }];
        
        //已读按钮
        [_isreadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.equalTo(superView.mas_left).offset(0);
            make.top.equalTo(superView.mas_top).offset(0);
            make.width.equalTo(@31);
            make.height.equalTo(@31);
        }];
        
        if ([reuseIdentifier isEqualToString:TEACHERCELLID]) { //老师身份
            
            //已读人数
            _readCountLabel = [[UILabel alloc] init];
            _readCountLabel.font = [UIFont systemFontOfSize:12];
            _readCountLabel.textColor = RGBACOLOR(59, 59, 59, 1);
            [superView addSubview:_readCountLabel];
            
            [_readCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.left.equalTo(superView.mas_left).offset(12);
                make.top.equalTo(self ->_contentLabel.mas_bottom).offset(13);
                make.width.equalTo(@85);
                make.bottom.equalTo(superView.mas_bottom).offset(-12);
            }];
            
            [_sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self->_readCountLabel.mas_right).offset(8);
                make.top.equalTo(self->_contentLabel.mas_bottom).offset(13);
                make.bottom.equalTo(superView.mas_bottom).offset(-12);
                make.right.equalTo(self->_picImageView.mas_left).offset(-15);
            }];
            
        }else if([reuseIdentifier isEqualToString:CLASSCELLID]){
        
            [_sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
                make.left.equalTo(superView.mas_left).offset(12);
                make.top.equalTo(self->_contentLabel.mas_bottom).offset(13);
                make.bottom.equalTo(superView.mas_bottom).offset(-12);
                make.right.equalTo(self->_picImageView.mas_left).offset(-15);
            }];
        }
        
    }
    
    return self;
}

-(void)updateCellWithModel:(homework *)model{
    
    if (model.title.length == 0) {
        _subLabel.text = @"通知";
    }else{
        _subLabel.text = model.title;
    }
    
    
    if (model.isread == nil) {  //这种情况是学生后加入的班级
        
        //_isreadBtn.hidden = YES;
        
    }else{
    
        if ([model.isread integerValue] == 0) {
            _isreadBtn.hidden = NO;
        }else if([model.isread integerValue] == 1){
            _isreadBtn.hidden = YES;
        }
    }

    NSString *timeStr = model.jointime;
    timeStr = [Utils getDataStringWithoutSecondString:model.jointime];

    _timeLabel.text = timeStr;
    
    if (model.scontent.length > 0) {
        
        _contentLabel.text = model.scontent;
    }else{
        _contentLabel.text = @"通知内容,请点击查看图片信息";
    }
    
    //已读人数

    NSString *str = [NSString stringWithFormat:@"已读:  %d/%d",(int)model.readnum,(int)model.allnum];
    
    NSRange range = [str rangeOfString:@"/"];
    CGFloat len = range.location - 4;
    NSRange newRange = NSMakeRange(4, len);
    
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attriStr addAttributes:@{NSForegroundColorAttributeName: RGBACOLOR(56, 173, 255, 1)} range:newRange];
    
    _readCountLabel.attributedText = attriStr;
    
    
    //来源
    if (model.ismyPub == 1) {
        _sourceLabel.text = [NSString stringWithFormat:@"发送对象:  %@",model.tousername];
    }else{
        _sourceLabel.text = [NSString stringWithFormat:@"发布者:  %@   %@",model.classname,model.username];
    }
    _sourceLabel.textAlignment=NSTextAlignmentLeft;
    NSString *fieldStr = model.fileid;
    
    NSArray *images;
    
    if (fieldStr.length > 0) { //有图片的情况
        
        _picImageView.hidden = NO;
        
        images = [fieldStr componentsSeparatedByString:@","];
        _imageArr = images;
        count = (int)images.count;
        NSString *countStr = [NSString stringWithFormat:@"%lu张",(unsigned long)images.count];
        NSString *logoStr = images[0];
        
         if (![logoStr hasPrefix:@"http"] && logoStr) {
            logoStr = [PICPERFIXURL stringByAppendingString:logoStr];
            
            logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
        }
        
        if (images.count == 1) {
            
            _picImageView.numberBtn.hidden = YES;
        }else if(images.count > 1){
            _picImageView.numberBtn.hidden = NO;
            [_picImageView.numberBtn setTitle:countStr forState:UIControlStateNormal];
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_picImageView addGestureRecognizer:tap];
        
        
        [_picImageView.taskImageView sd_setImageWithURL:[NSURL URLWithString:logoStr] placeholderImage:[UIImage imageNamed:@"emptyPic"]];
    }else{ //没有图片的情况

        _picImageView.taskImageView.image = [UIImage imageNamed:@"nopic_back"];
        
        for (UIGestureRecognizer *tap in _picImageView.gestureRecognizers) {
            [tap removeTarget:self action:@selector(tapAction:)];
        }
        
        _picImageView.numberBtn.hidden = YES;
    }
    
}
-(void)updateOACellWithModel:(mySendOANotiModel *)model{
    
    if (model.title.length == 0) {
        _subLabel.text = @"通知";
    }else{
        _subLabel.text = model.title;
    }

    
    NSString *timeStr = model.jointime;
    
    timeStr = [Utils getDataStringWithoutSecondString:model.jointime];
    
    _timeLabel.text = timeStr;
    
    if (model.scontent.length > 0) {
        
        _contentLabel.text = model.scontent;
    }else{
        _contentLabel.text = @"通知内容,请点击查看图片信息";
    }
    
    //已读人数
    
    NSString *str = [NSString stringWithFormat:@"已读:  %d/%d",(int)model.readuser,(int)model.alluser];
    
    NSRange range = [str rangeOfString:@"/"];
    CGFloat len = range.location - 4;
    NSRange newRange = NSMakeRange(4, len);
    
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:str];
    [attriStr addAttributes:@{NSForegroundColorAttributeName: RGBACOLOR(56, 173, 255, 1)} range:newRange];
    
    _readCountLabel.attributedText = attriStr;
    
    
    _sourceLabel.text = [NSString stringWithFormat:@"发送对象:  %@",model.tousername];
    
    _sourceLabel.textAlignment=NSTextAlignmentLeft;
    NSString *fieldStr = model.fileid;
    
    NSArray *images;
    
    if (fieldStr.length > 0) { //有图片的情况
        
        _picImageView.hidden = NO;
        
        images = [fieldStr componentsSeparatedByString:@","];
        _imageArr = images;
        count = (int)images.count;
        NSString *countStr = [NSString stringWithFormat:@"%lu张",(unsigned long)images.count];
        NSString *logoStr = images[0];
        
         if (![logoStr hasPrefix:@"http"] && logoStr) {
            logoStr = [PICPERFIXURL stringByAppendingString:logoStr];
            
            logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
        }
        
        if (images.count == 1) {
            
            _picImageView.numberBtn.hidden = YES;
        }else if(images.count > 1){
            _picImageView.numberBtn.hidden = NO;
            [_picImageView.numberBtn setTitle:countStr forState:UIControlStateNormal];
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_picImageView addGestureRecognizer:tap];
        
        
        [_picImageView.taskImageView sd_setImageWithURL:[NSURL URLWithString:logoStr] placeholderImage:[UIImage imageNamed:@"nopic_back"]];
    }else{ //没有图片的情况
        
        _picImageView.taskImageView.image = [UIImage imageNamed:@"nopic_back"];
        
        for (UIGestureRecognizer *tap in _picImageView.gestureRecognizers) {
            [tap removeTarget:self action:@selector(tapAction:)];
        }
        
        _picImageView.numberBtn.hidden = YES;
    }
    
}


-(void)updateReceveOACellWithModel:(mySendOANotiModel *)model{
    
    if (model.title.length == 0) {
        _subLabel.text = @"通知";
    }else{
        _subLabel.text = model.title;
    }
    

        
    if (model.isread == 0) {
        
        _isreadBtn.hidden = NO;
        
    }else if(model.isread == 1){
        
        _isreadBtn.hidden = YES;
    }
    
    NSString *timeStr = model.jointime;

     timeStr = [Utils getDataStringWithoutSecondString:model.jointime];
    
    if (timeStr.length > 3) {
        
        timeStr =[timeStr substringToIndex:timeStr.length-3];
    }
    _timeLabel.text = timeStr;
    
    if (model.scontent.length > 0) {
        
        _contentLabel.text = model.scontent;
    }else{
        _contentLabel.text = @"通知内容,请点击查看图片信息";
    }
    
    //已读人数
    
//    NSString *str = [NSString stringWithFormat:@"已读:  %d/%d",(int)model.readuser,(int)model.alluser];
//    
//    NSRange range = [str rangeOfString:@"/"];
//    CGFloat len = range.location - 4;
//    NSRange newRange = NSMakeRange(4, len);
//    
//    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:str];
//    [attriStr addAttributes:@{NSForegroundColorAttributeName: RGBACOLOR(56, 173, 255, 1)} range:newRange];
//    
//    _readCountLabel.attributedText = attriStr;
    
    
    _sourceLabel.text = [NSString stringWithFormat:@"发布者:  %@",model.sendschoolusername];
    
    _sourceLabel.textAlignment=NSTextAlignmentLeft;
    NSString *fieldStr = model.fileid;
    
    NSArray *images;
    
    if (fieldStr.length > 0) { //有图片的情况
        
        _picImageView.hidden = NO;
        
        images = [fieldStr componentsSeparatedByString:@","];
        _imageArr = images;
        count = (int)images.count;
        NSString *countStr = [NSString stringWithFormat:@"%lu张",(unsigned long)images.count];
        NSString *logoStr = images[0];
        
         if (![logoStr hasPrefix:@"http"] && logoStr) {
            logoStr = [PICPERFIXURL stringByAppendingString:logoStr];
            
            logoStr = [NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
        }
        
        if (images.count == 1) {
            
            _picImageView.numberBtn.hidden = YES;
        }else if(images.count > 1){
            _picImageView.numberBtn.hidden = NO;
            [_picImageView.numberBtn setTitle:countStr forState:UIControlStateNormal];
        }
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_picImageView addGestureRecognizer:tap];
        
        
        [_picImageView.taskImageView sd_setImageWithURL:[NSURL URLWithString:logoStr] placeholderImage:[UIImage imageNamed:@"nopic_back"]];
    }else{ //没有图片的情况
        
        _picImageView.taskImageView.image = [UIImage imageNamed:@"nopic_back"];
        
        for (UIGestureRecognizer *tap in _picImageView.gestureRecognizers) {
            [tap removeTarget:self action:@selector(tapAction:)];
        }
        
        _picImageView.numberBtn.hidden = YES;
    }
    
}

-(void)tapAction:(UIGestureRecognizer *)tap{
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    browser.sourceImagesContainerView = _picImageView;
    browser.imageCount = count;
    
    browser.currentImageIndex = 0;
    browser.delegate = self;
    [browser show];
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = _imageArr[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
        logoStr = [PICPERFIXURL stringByAppendingString:str];
        logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    __block UIImage *_image;
    [imageView sd_setImageWithURL:[NSURL URLWithString:logoStr] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        _image = image;
    }];
    return _image;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    NSString *str = _imageArr[index];
    NSString *logoStr;
    if ([flagStr isEqualToString:@"1"]) {
      logoStr = [PICPERFIXURL stringByAppendingString:str];
    }else
    {
       logoStr = [PICPERFIXURL stringByAppendingString:str];
       logoStr=[NSString stringWithFormat:@"%@&s=%d",logoStr,ImageWidht];
    }
    
    
    NSURL *url = [NSURL URLWithString:logoStr];
    return url;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
